//
//  SMDataManage.m
//  SaveMoney
//
//  Created by user on 2018/12/6.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMDataManage.h"

#import "YYModel/YYModel/YYModel.h"
#import "NSString+JSON.h"

@implementation SMDataManage

+ (instancetype)sharedStorage {
    static SMDataManage *storage;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        storage = [[self alloc] init];
    });
    return storage;
}

- (NSString *)upJson {
    
    NSArray *dict = [self upLoadData];
    
    NSString *json = [NSString convertToJSONData:dict];
    
    _upJson = json;
    
    return _upJson;
}
 
- (NSArray *)upLoadData {
//    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    NSMutableArray *proArray = [[NSMutableArray alloc] init];
    NSArray *project = [SMMoneyModel findAll];
    for (SMMoneyModel *model in project) {
        NSDictionary *dic = @{@"saveMoneyTitle":model.saveMoneyTitle?:@"",
                              @"saveMoneyType":@(model.saveMoneyType),
                              @"totalMoney": @(model.totalMoney),
                              @"saveMoneyDay": @(model.saveMoneyDay),
                              @"alreadyMoney": @(model.alreadyMoney),
                              @"everyDayMoney": @(model.everyDayMoney),
                              @"backImage": model.backImage?:@"",
                              @"creationTime": model.creationTime?:@"",
                              @"saveMessageList":[self saveMoneyTitle:model.saveMoneyTitle],
                              @"phone": SMUserInfoModel.sharedStorage.phone
                              };
        [proArray addObject:dic];
    }
    
    return proArray;
}

- (NSArray *)saveMoneyTitle:(NSString *)saveMoneyTitle {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    NSArray *list = [SMSaveMoneyMessageModel findByCriteria:[NSString stringWithFormat:@"Where saveMoneyTitle = '%@'",saveMoneyTitle]];
    for (SMSaveMoneyMessageModel *model in list) { 
        NSDictionary *dic = @{@"type":@(model.type),
                              @"userKey":model.userKey?:@"",
                              @"projectId": @(model.projectId),
                              @"creationTime": model.creationTime?:@"",
                              @"total":@(model.total),
                              @"bitMoney": @(model.bitMoney),
                              @"saveMoney": @(model.saveMoney),
                              @"costMoney": @(model.costMoney),
                              @"message":model.message?:@"",
//                              @"saveMoneyTitle":model.saveMoneyTitle
                              };
        [array addObject:dic];
    }
    return array;
}

- (void)saveDataWithArray:(NSArray *)array {
    NSMutableArray *saveArray = [[NSMutableArray alloc] init];
    NSMutableArray *messageListArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in array) {
        SMMoneyModel *model = [[SMMoneyModel alloc] init];
        model.saveMoneyTitle = dict[@"saveMoneyTitle"];
        model.saveMoneyType = [dict[@"saveMoneyType"] integerValue];
        model.totalMoney = [dict[@"totalMoney"] integerValue];
        model.saveMoneyDay = [dict[@"saveMoneyDay"] integerValue];
        model.alreadyMoney = [dict[@"alreadyMoney"] integerValue];
        model.everyDayMoney = [dict[@"everyDayMoney"] integerValue];
        model.backImage = dict[@"backImage"];
        model.creationTime = dict[@"creationTime"];
        model.userKey = dict[@"phone"];
        NSArray *saveMessageList = dict[@"saveMessageList"];
        for (NSDictionary *messageDict in saveMessageList) {
            SMSaveMoneyMessageModel *messageModel = [[SMSaveMoneyMessageModel alloc] init];
            messageModel.type = [messageDict[@"type"] integerValue];
            messageModel.userKey = messageDict[@"userKey"];
            messageModel.projectId = [messageDict[@"projectId"] integerValue];
            messageModel.creationTime = messageDict[@"creationTime"];
            messageModel.total = [messageDict[@"total"] integerValue];
            messageModel.bitMoney = [messageDict[@"bitMoney"] integerValue];
            messageModel.saveMoney = [messageDict[@"saveMoney"] integerValue];
            messageModel.costMoney = [messageDict[@"costMoney"] integerValue];
            messageModel.message = messageDict[@"message"];
            messageModel.saveMoneyTitle = model.saveMoneyTitle;
            [messageListArray addObject:messageModel];
        }
        [saveArray addObject:model];
    }
    
    if ([SMMoneyModel saveObjects:saveArray]) {
        [SMSaveMoneyMessageModel saveObjects:messageListArray];
    }
    
}


- (void)removeData {
    [SMMoneyModel clearTable];
    [SMSaveMoneyMessageModel clearTable];
}



@end
